package com.nbstar.film.spider.engine;

import com.nbstar.film.spider.domain.FilmSpiderDomain;
import com.nbstar.film.spider.excel.CommonExcelReader;
import com.nbstar.film.spider.excel.IExcelWriter;
import com.nbstar.film.spider.excel.YunNanExcelReader;
import com.nbstar.film.spider.excel.YunNanExcelWriter;
import com.nbstar.film.spider.pojo.FilmInfo;
import com.nbstar.film.spider.spider.*;
import com.nbstar.film.spider.thread.ProxyThread;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Slf4j
@Data
public class YNSpiderEngine extends BaseSpiderEngine {

    private TVCatSpider.MediaType type = TVCatSpider.MediaType.FILM;

    public void setType(TVCatSpider.MediaType type) {
        this.type = type;
    }
    private int  readExcelCol = 0;

    @Override
    public void executeDomain(FilmSpiderDomain filmSpiderDomain) {
        FilmSpider spider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_BAIKE);
        FilmInfo filmInfo = ((BaiKeSpider) spider).run(filmSpiderDomain.getRaw().getFilmName());

        if (StringUtils.isEmpty(filmInfo.getArriveAddress()) && StringUtils.isEmpty(filmInfo.getArriveDate())) {
            FilmSpider tvCatSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_TVMAO_FILM);;

            if (type == TVCatSpider.MediaType.TV) {
                tvCatSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV);;
            }

            ((TVCatSpider) tvCatSpider).setRetry(0);
            filmInfo = ((TVCatSpider) tvCatSpider).run(filmSpiderDomain.getRaw().getFilmName());
        }

        if (StringUtils.isEmpty(filmInfo.getArriveAddress()) && StringUtils.isEmpty(filmInfo.getArriveDate())) {
            FilmSpider youKuSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_YOUKU);;
            ((YouKuSpider) youKuSpider).setRetry(0);
            filmInfo = ((YouKuSpider) youKuSpider).run(filmSpiderDomain.getRaw().getFilmName());
        }

        if (StringUtils.isEmpty(filmInfo.getArriveAddress()) && StringUtils.isEmpty(filmInfo.getArriveDate())) {
            FilmSpider doubanSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_DOUBAN);;
            ((DoubanFilmSpider) doubanSpider).setRetry(0);
            filmInfo = ((DoubanFilmSpider) doubanSpider).runProxy(filmSpiderDomain.getRaw().getFilmName());
        }

        //logger.debug(filmInfo.toString());
        if (StringUtils.isEmpty(filmInfo.getArriveAddress()) && StringUtils.isEmpty(filmInfo.getArriveDate())) {
            FilmSpider maoYanSpider =  spiderMap.get(FilmSpiderDomain.SPIDER_FROM_MAOYAN);;
            ((MaoYanSpider) maoYanSpider).setRetry(0);
            filmInfo = ((MaoYanSpider) maoYanSpider).run(filmSpiderDomain.getRaw().getFilmName());
        }
        log.debug(filmInfo.toString());
        filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_BAIKE, filmInfo);

    }

    @Override
    public boolean checkDomainSucc(FilmSpiderDomain domain) {
        return !StringUtils.isEmpty(domain.getSpiderResult().get(FilmSpiderDomain.SPIDER_FROM_BAIKE).getArriveAddress())
                || !StringUtils.isEmpty(domain.getSpiderResult().get(FilmSpiderDomain.SPIDER_FROM_BAIKE).getArriveDate());
    }

    @Override
    public void initEngine() {


        registSpider(FilmSpiderDomain.SPIDER_FROM_DOUBAN, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_DOUBAN));
        registSpider(FilmSpiderDomain.SPIDER_FROM_IMDB, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_IMDB));
        registSpider(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV));
        registSpider(FilmSpiderDomain.SPIDER_FROM_TVMAO_FILM, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_TVMAO_FILM));
        registSpider(FilmSpiderDomain.SPIDER_FROM_MAOYAN, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_MAOYAN));
        registSpider(FilmSpiderDomain.SPIDER_FROM_BAIKE, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_BAIKE));
        registSpider(FilmSpiderDomain.SPIDER_FROM_YOUKU, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_YOUKU));
//获取代理
        ProxyThread proxyThread = new ProxyThread();
        proxyThread.start();

        this.excelWriter  = new YunNanExcelWriter(resultSheetName, resultExcelName);

        this.excelReader = new CommonExcelReader();
        ((CommonExcelReader) this.excelReader).setColIndex(readExcelCol);
        excelReader.setFilmSpiderQueue(filmSpiderQueue);

    }
}
